<?php /** @noinspection ALL */

//用户账户
namespace app\admin\controller;


use think\facade\Db;

class UserAccount {

	function show($page=1,$pageSize=10){
		$input=input();

		$userinfo=Pubilc::check_jwt($input['token']);

		if(!$userinfo)return json(['code' => 0,'msg'=>'请重新登录']);

		$id=$userinfo->userId;

		$type=$userinfo->type;

		$condition = ' and 1=1';

		if (!empty($input['userinfo'])) {
			$condition .= " and (u.nick_name like '%{$input['userinfo']}%' or u.tel like '%{$input['userinfo']}%' or u.real_name like '%{$input['userinfo']}%' ) ";
		}

		if($type==2){//代理

			$list = Db::query("select u.nick_name,u.tel,u.real_name,user_account.* from user_account join system_user as u on u.id = user_account.uid where 1".$condition." and u.agent_id ='{$id}' order by user_account.id desc limit ".(($page-1)*$pageSize).",$pageSize");

			$total_num = Db::query("select count(user_account.id) as sum from user_account join system_user as u on u.id = user_account.uid where 1".$condition." and u.agent_id ='{$id}' ");

		}else{
			$list = Db::query("select u.nick_name,u.tel,u.real_name,user_account.* from user_account join system_user as u on u.id = user_account.uid where 1".$condition." order by user_account.id desc limit ".(($page-1)*$pageSize).",$pageSize");

			$total_num = Db::query("select count(user_account.id) as sum from user_account join system_user as u on u.id = user_account.uid where 1".$condition);
		}

		if(!empty($list)){

			for ($i = 0; $i < count($list); $i++) {

				$list[$i]['type1']=1;

				$list[$i]['type2']=1;

			}
		}

		if($type==2){
			$usertype=2;
		}else{
			$usertype=1;
		}

		return json(['code' => 1, 'data' => $list,'total_num'=>$total_num[0]['sum'],'usertype'=>$usertype]);
	}

	function post(){
		$input=input();

		if(empty($input['id']))return json(['code' => 0, 'msg' => '参数错误']);

		$user_account=Db::table('user_account')->where('id', $input['id'])->find();

		if ($input['frozen_amount']<0) return json(['code' => 0, 'msg' => '请输入正确的冻结金额']);

		if ($input['frozen_gold']<0) return json(['code' => 0, 'msg' => '请输入正确的冻结金币']);

		if ($input['total_recharge']<0) return json(['code' => 0, 'msg' => '请输入正确的充值总额']);

		if ($input['total_withdrawal']<0) return json(['code' => 0, 'msg' => '请输入正确的提现总额']);

		if ($input['total_transaction']<0) return json(['code' => 0, 'msg' => '请输入正确的交易总额']);

		if ($input['total_fee']<0) return json(['code' => 0, 'msg' => '请输入正确的总手续费']);

		if (!empty($input['type1'])) {
			if ($input['type1'] == 1) {//修改金额

				$edit_type=7;

				if ($input['amount']<0) return json(['code' => 0, 'msg' => '请输入正确的金额']);

				$amount = $input['amount'];
			}
			if ($input['type1'] == 2) {//增加金额
				$edit_type=5;

				$amount = floatval($user_account['amount']) + floatval($input['amount']);
			}
			if ($input['type1'] == 3) {//减少金额
				$edit_type=6;

				if (floatval($user_account['amount']) < floatval($input['amount'])) return json(['code' => 0, 'msg' => "金额不足" . floatval($input['amount'])]);

				$amount = floatval($user_account['amount']) - floatval($input['amount']);
			}
		} else {
			return json(['code' => 0, 'msg' => "请选择修改金额方式"]);
		}

		if (!empty($input['type2'])) {
			if ($input['type2'] == 1) {//修改金币

				if ($input['gold']<0) return json(['code' => 0, 'msg' => '请输入正确的金币']);

				$gold = $input['gold'];
			}
			if ($input['type2'] == 2) {//增加金币
				$gold = floatval($user_account['gold']) + floatval($input['gold']);
			}
			if ($input['type2'] == 3) {//减少金币

				if (floatval($user_account['gold']) < floatval($input['gold'])) return json(['code' => 0, 'msg' => "金币不足" . floatval($input['gold'])]);

				$gold = floatval($user_account['gold']) - floatval($input['gold']);
			}
		} else {
			return json(['code' => 0, 'msg' => "请选择修改金额方式"]);
		}

		if($amount!=$user_account['amount']){

			$number_mooney=$input['amount'];//操作金额

			$edit_after=$amount;

			$money_log=[
				'uid'=>$user_account['uid'],
				'number'=>$number_mooney,
				'type'=>$edit_type,
				'edit_after'=>$edit_after,
				'add_time'=>time(),
			];

			Db::name('money_log')->insert($money_log);
		}


		$data=[
			'amount'=>$amount,
			'gold'=>$gold,
			'frozen_amount'=>$input['frozen_amount'],
			'frozen_gold'=>$input['frozen_gold'],
			'total_recharge'=>$input['total_recharge'],
			'total_withdrawal'=>$input['total_withdrawal'],
			'total_transaction'=>$input['total_transaction'],
			'total_fee'=>$input['total_fee'],
		];

		$r=Db::name('user_account')->where('id',$input['id'])->update($data);

		return json(['code' => 1, 'msg' => "编辑成功"]);
	}

}